<template>
	<div class="app-container">
		<el-row :gutter="10" class="mb8" style="height: 30px;">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['orders:detail:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['orders:detail:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['orders:detail:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['orders:detail:export']">导出</el-button>
			</el-col>

				 <div style="margin-left: 815px;">
				  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
				  	<el-form-item label="采购申请编号" prop="purchaseRequestId" label-width="50">
				  		<el-input v-model="queryParams.purchaseRequestId" placeholder="请输入采购申请编号" clearable
				  			@keyup.enter.native="handleQuery" />
				  	</el-form-item>
				  	<el-form-item>
				  		<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				  		<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
				  	</el-form-item>
				  </el-form>
				  </div>
	 
		</el-row>

		<el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange" border>
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="采购申请行状态" align="center" prop="demandLineStatus" width="180">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.demand_line_status" :value="scope.row.demandLineStatus" />
				</template>
			</el-table-column>
			<el-table-column label="采购申请编号" align="center" prop="purchaseRequestId" width="180" />
			<el-table-column label="需求行号" align="center" prop="demandLineNumber" />
			<el-table-column label="采购类型" align="center" prop="typePurchase" width="180">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.sys_type_purchase" :value="scope.row.typePurchase" />
				</template>
			</el-table-column>
			<el-table-column label="申请日期" align="center" prop="applicationDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="申请人" align="center" prop="person" />
			<el-table-column label="产品编号" align="center" prop="productNumber" width="180" />
			<el-table-column label="产品名称" align="center" prop="productName" />
			<el-table-column label="单位" align="center" prop="productUnit">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.sys_unit" :value="scope.row.productUnit" />
				</template>
			</el-table-column>
			<el-table-column label="需求数量" align="center" prop="requiredQuantity" />
			<el-table-column label="期望到货日期" align="center" prop="expectedDeliveryDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.expectedDeliveryDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="期望供应商id" align="center" prop="expectedSupplierId" />
			<el-table-column label="申请单id" align="center" prop="requestId" />
			<el-table-column label="询报价需求申请编号" align="center" prop="inquiryDemandNumber" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['orders:detail:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['orders:detail:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改申请明细对话框 -->
		<el-dialog :title="title" :visible.sync="open" :fullscreen="true" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="采购申请行状态" prop="demandLineStatus" label-width="50" style="margin: 0px;" />
						<div style="width: 40px;">
							<el-radio-group v-model="form.demandLineStatus">
								<el-radio v-for="dict in dict.type.demand_line_status" :key="dict.value"
									:label="parseInt(dict.value)">{{dict.label}}</el-radio>
							</el-radio-group>
							</el-radio-group>
						</div>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="采购申请编号" prop="purchaseRequestId" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.purchaseRequestId" placeholder="请输入采购申请编号" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="需求行号" prop="demandLineNumber" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.demandLineNumber" placeholder="请输入需求行号" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="采购类型" prop="typePurchase" label-width="50" style="margin: 0px;" />
						<el-select v-model="form.typePurchase" placeholder="请选择采购类型" style="width: 25%;">
							<el-option v-for="dict in dict.type.sys_type_purchase" :key="dict.value" :label="dict.label"
								:value="parseInt(dict.value)"></el-option>
						</el-select>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="申请日期" prop="applicationDate" label-width="50" style="margin: 0px;" />
						<el-date-picker clearable v-model="form.applicationDate" type="date" value-format="yyyy-MM-dd" style="width: 25%;"
							placeholder="请选择申请日期">
						</el-date-picker>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="申请人" prop="person" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.person" placeholder="请输入申请人" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="申请部门" prop="" label-width="50" style="margin: 0px;" />
						<el-input placeholder="请输入申请部门" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="产品编号" prop="productNumber" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.productNumber" placeholder="请输入产品编号" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="产品名称" prop="productName" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.productName" placeholder="请输入产品名称" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="单位" prop="productUnit" label-width="50" style="margin: 0px;" />
						<el-select v-model="form.productUnit" placeholder="请选择单位" style="width: 25%;">
							<el-option v-for="dict in dict.type.sys_unit" 
							:key="dict.value"
							:label="dict.label"
							:value="dict.value"></el-option>
						</el-select>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="需求数量" prop="requiredQuantity" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.requiredQuantity" placeholder="请输入需求数量" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="期望供应商" prop="expectedSupplierId" label-width="50" style="margin: 0px;" />
						<el-input v-model="form.expectedSupplierId" placeholder="请输入期望供应商" style="width: 25%;"/>
					</el-col>
				</el-row>

				<el-row style="margin-bottom: 20px;">
					<el-col>
						<el-form-item label="期望到货日期" prop="expectedDeliveryDate" label-width="50"
							style="margin: 0px;" />
						<el-date-picker clearable v-model="form.expectedDeliveryDate" type="date" style="width: 25%;"
							value-format="yyyy-MM-dd" placeholder="请选择期望到货日期">
						</el-date-picker>
					</el-col>
				</el-row>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listDetail,
		getDetail,
		delDetail,
		addDetail,
		updateDetail
	} from "@/api/orders/detail";

	export default {
		name: "Detail",
		dicts: ['sys_unit', 'demand_line_status', 'sys_type_purchase'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 申请明细表格数据
				detailList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					purchaseRequestId: null,
					typePurchase: null,
					applicationDate: null,
					person: null,
					productNumber: null,
					productName: null,
					productUnit: null,
					demandLineNumber: null,
					requiredQuantity: null,
					expectedDeliveryDate: null,
					expectedSupplierId: null,
					demandLineStatus: null,
					requestId: null,
					inquiryDemandNumber: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					applicationDate: [{
						required: true,
						message: "申请日期不能为空",
						trigger: "blur"
					}],
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询申请明细列表 */
			getList() {
				this.loading = true;
				listDetail(this.queryParams).then(response => {
					this.detailList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					detailId: null,
					purchaseRequestId: null,
					typePurchase: null,
					applicationDate: null,
					person: null,
					productNumber: null,
					productName: null,
					productUnit: null,
					demandLineNumber: null,
					requiredQuantity: null,
					expectedDeliveryDate: null,
					expectedSupplierId: null,
					demandLineStatus: null,
					requestId: null,
					inquiryDemandNumber: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.detailId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加申请明细";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const detailId = row.detailId || this.ids
				getDetail(detailId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改申请明细";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.detailId != null) {
							updateDetail(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addDetail(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const detailIds = row.detailId || this.ids;
				this.$modal.confirm('是否确认删除申请明细编号为"' + detailIds + '"的数据项？').then(function() {
					return delDetail(detailIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('orders/detail/export', {
					...this.queryParams
				}, `detail_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>